﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;

using TRPOP.CMS.RegAssembly;
using TRPOP.Common;

namespace TRPOP.CMS.Template.GenerateCS
{

    public partial class ConvertTags
    {
        #region 全局属性
        /// <summary>
        /// 解析某个标签的开始注释
        /// </summary>
        private const string strTagAnayliseBegin = "/**** 处理{0}......begin    */{1}";
        /// <summary>
        /// 解析某个标签的结束注释
        /// </summary>
        private const string strTagAnayliseEnd = "/**** 处理{0}......end    */{1}";
        /// <summary>
        /// 简单的注释
        /// </summary>
        private const string strTagAnaylisenote = "// 处理{0} {1}";
        /// <summary>
        /// 找到每个标签
        /// </summary>
        private static Tags_AllTag oTags_AllTag = new Tags_AllTag();
        /// <summary>
        /// 找到 Attribute
        /// </summary>
        private static TRPOP.CMS.RegAssembly.Tags_Attribute oTags_Attribute = new Tags_Attribute();
        /// <summary>
        /// 解析CMS标签（CMS标记的标签）
        /// </summary>
        private static TRPOP.CMS.RegAssembly.Tags_CMS oTags_CMS = new Tags_CMS();
        /// <summary>
        /// 找到所有的变量
        /// </summary>
        private static TRPOP.CMS.RegAssembly.Tags_AllTypeVar oTags_AllTypeVar = new Tags_AllTypeVar();
        /// <summary>
        /// 所有的变量 不含符号，例如： [ {
        /// </summary>
        private static TRPOP.CMS.RegAssembly.Tags_AllTypeVarWithNoSign oTags_AllTypeVarWithNoSign = new Tags_AllTypeVarWithNoSign();
        /// <summary>
        /// 解析 var name="cfg_webname" value="约克软件" 
        /// </summary>
        private static TRPOP.CMS.RegAssembly.Tags_Var oTags_Var = new Tags_Var();
        /// <summary>
        /// 解析 op exp='......'
        /// </summary>
        private static TRPOP.CMS.RegAssembly.Tags_Op oTags_Op = new Tags_Op();
        /// <summary>
        /// 找到Loop<![CDATA[ <loop name="List" var="var" key="key" > ]]>
        /// </summary>
        private static TRPOP.CMS.RegAssembly.Tags_oneLoop oTags_oneLoop = new Tags_oneLoop();
        /// <summary>
        ///  找到if系列的标签，并找到test
        /// </summary>
        private static TRPOP.CMS.RegAssembly.Tags_IfGramar oTags_IfGramar = new Tags_IfGramar();
        /// <summary>
        /// 找到函数
        /// </summary>
        public  static TRPOP.CMS.RegAssembly.Tags_Func oTags_Func = new Tags_Func();
        #endregion

        #region 方法

        /// <summary>
        /// 找到第一个可解析的标签
        /// </summary>
        /// <param name="strHtml"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static Match GetTopOneAnalysisTag(string strHtml, ref int index)
        {
            Match oMatch = oTags_AllTag.Match(strHtml);
            if (oMatch.Success)
            {
                if (oMatch.Groups["TagName"].Value.IsEmpty() == false)
                    index = 0;
                else if (oMatch.Groups["func"].Value.IsEmpty() == false)
                    index = 2;
                else
                    index = 1;
            }
            else
                index = -1;
            return oMatch;
        }

        /// <summary>
        /// 根据字符串获取所有的属性值
        /// </summary>
        /// <param name="strHtmlContent"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetAttribute(string strHtmlContent)
        {
            Dictionary<string, string> odicAttribute = new Dictionary<string, string>();

            MatchCollection oMatchCollection = oTags_Attribute.Matches(strHtmlContent);
            foreach (Match oMatch in oMatchCollection)
            {
                if (!odicAttribute.ContainsKey(oMatch.Groups["name"].Value.Trim().ToLower()))
                {
                    odicAttribute.Add(oMatch.Groups["name"].Value.Trim().ToLower(), oMatch.Groups["value"].Value.Trim());
                }
                else
                {
                    odicAttribute[oMatch.Groups["name"].Value.Trim().ToLower()] = oMatch.Groups["value"].Value.Trim();
                }
            }

            return odicAttribute;
        }

        /// <summary>
        /// 获取属性，例如 testAttr='...'
        /// </summary>
        /// <param name="strHtmlContent"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetAttributeByGingleQuotes(string strHtmlContent)
        {
            Dictionary<string, string> odicAttribute = new Dictionary<string, string>();

            Regex oRegex = new Regex(@"(?<name>\w*)='(?<value>[\s\S]+?)'");
            MatchCollection oMatchCollection = oRegex.Matches(strHtmlContent);
            foreach (Match oMatch in oMatchCollection)
            {
                if (!odicAttribute.ContainsKey(oMatch.Groups["name"].Value.Trim().ToLower()))
                {
                    odicAttribute.Add(oMatch.Groups["name"].Value.Trim().ToLower(), oMatch.Groups["value"].Value.Trim());
                }
                else
                {
                    odicAttribute[oMatch.Groups["name"].Value.Trim().ToLower()] = oMatch.Groups["value"].Value.Trim();
                }
            }

            return odicAttribute;
        }

        #endregion
    }
}